import { createBrowserHistory } from "history";
import React, { Component } from "react";
import { Redirect, Route, Router, Switch } from "react-router-dom";
import ArticlePage from "./pages/articlePage";
import DashboardPage from "./pages/dashboardPage";
import LoginPage from "./pages/loginPage";
import Layout from "./shared/layout";
import RouteGuard from "./shared/routeGuaed";
import isLogin from "./utils/isLogin";

export const history = createBrowserHistory();

export default class App extends Component {
  render() {
    return (
      <Router history={history}>
        <Switch>
          <Redirect from="/" to="/login" exact />
          <Route path="/login" component={LoginPage} />
          <Route path="/admin">
            <RouteGuard
              guards={[isLogin]}
              onRejected={() => <Redirect to="/login" push={false}></Redirect>}
            >
              <Layout>
                <Switch>
                  <Redirect from="/admin" to="/admin/dashboard" exact />
                  <Route
                    path="/admin/dashboard"
                    component={DashboardPage}
                  ></Route>
                  <Route path="/admin/article" component={ArticlePage} />
                </Switch>
              </Layout>
            </RouteGuard>
          </Route>
        </Switch>
      </Router>
    );
  }
}
